package com.example.mybatis.mapper;

import com.example.mybatis.model.UserInfo;
import lombok.Data;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserInfoMapper {
    /**
     * 尽量避免使用*
     * @return
     */
    //查询所有⽤⼾
    @Select("select id, username, age, gender, phone, delete_flag, create_time, update_time from user_info")
    List<UserInfo> userInfoAll3();

    /*
          开启驼峰命名 ,在yml中写注解方式
     * */
    @Select("select id, username, age, gender, phone, delete_flag, create_time, update_time from user_info")
    List<UserInfo> userInfoAll1();

    /*
    起别名方法
    */
    @Select("select id, username,password,age,gender,phone, " +
            "delete_flag as deleteFlag, create_time as createTime, update_time as updateTime " +
            " from user_info")
    List<UserInfo> userInfoAll2();

    /*
    指定结果映射关系
    */
    @Results({                //数据库字段名            创建的java对象的属性
            @Result(column = "delete_flag", property = "deleteFlag"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime"),
    })


    @Select("select id, username, age, gender, phone, delete_flag, create_time, update_time from user_info where id = #{id}")
    List<UserInfo> userInfoAlls(Integer id);

    @Insert("INSERT INTO user_info(username,password,age,gender) values (#{username},#{password},#{age},#{gender})")
    Integer insert(UserInfo userInfo);

    @Delete("DELETE from user_info where id =#{id}")
    Integer delete(Integer id);

    @Update("UPDATE user_info set username = #{username},password = #{password},age = #{age},gender = #{gender} " +
            "where id =#{id}")
    Integer updata(@Param("user") UserInfo userInfo);

    @Select("select id, username, age, gender, phone, delete_flag, create_time, update_time from user_info order by id ${key} limit 2")
    List<UserInfo> selectByOrder(String key);

    @Select("select id, username, age, gender, phone, delete_flag, create_time, update_time from user_info where username like concat('%',#{key},'%')")
    List<UserInfo> selectByLike(String key);

}
